﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace MDFGame
{
    [ObjectSystem]
    public class ResSystem : Model.System
    {
        public override void Create()
        {
            GameRoot.Scene.AddComponent<GlobalComponent>();
        }

        public void SetConfigData(Dictionary<string, string> data, string assetpath)
        {
            GlobalComponent _temp = GameRoot.Scene.GetComponent<GlobalComponent>();
            _temp.AssetPath = assetpath + "/";
            _temp.DBPath = data["CardDB"].ToString();
            _temp.WindowSizeX = Convert.ToInt32(data["WindowSizeX"]);
            _temp.WindowSizeY = Convert.ToInt32(data["WindowSizeY"]);
            _temp.GameCardSizeX = Convert.ToInt32(data["CardSizeX"]);
            _temp.GameCardSizeY = Convert.ToInt32(data["CardSizeX"]);
            _temp.SqlManager = new SqlManager(_temp.AssetPath + _temp.DBPath);//装载数据库所有卡片文本信息
        }
        public void SetUIConfigData(Dictionary<string, string> data, string assetpath)
        {
            UIViewComponent _temp = GameRoot.Scene.GetComponent<UIViewComponent>();
            _temp.packagePath = assetpath + "/" + data["UIPath"];
            data.Remove("UIPath");
            List<string> _ViewName = new List<string>();
            foreach (string key in data.Keys)
            {
                _ViewName.Add(key);
            }
            _temp.ViewNames = _ViewName;
        }

        public void SetCardEffect()
        {
            AssembliesComponent Asc = GameRoot.Scene.GetComponent<AssembliesComponent>();
            GlobalComponent _temp = GameRoot.Scene.GetComponent<GlobalComponent>();

            _temp.CardEffectDic = new Dictionary<string, Type>();
            if (Asc.AllAttributes.ContainsKey(typeof(CardEffectAttribute)))
            {
                foreach (Type type in Asc.AllAttributes[typeof(CardEffectAttribute)])
                {
                    object[] attrs = type.GetCustomAttributes(typeof(CardEffectAttribute), false);
                    if (attrs.Length == 0)
                    {
                        return;
                    }
                    CardEffectAttribute cardEffect = (CardEffectAttribute)attrs[0];
                    if (_temp.CardEffectDic.ContainsKey(cardEffect.Name))
                    {
                        Log.Error($"{cardEffect.Name}的效果描述函数出现了两次！");
                        continue;
                    }
                    _temp.CardEffectDic.Add(cardEffect.Name, type);
                }
            }
        }
    
        
        public override void Destory()
        {
            GameRoot.Scene.RemoveComponent<GlobalComponent>();
        }

    }
}
